package com.yanchangapp.ui.activity.purchase.apply;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.yanchangapp.R;
import com.yanchangapp.commom.LoadingType;
import com.yanchangapp.model.bean.GoodsApplyBean;
import com.yanchangapp.model.bean.SupplierBean;
import com.yanchangapp.ui.presenter.GoodsDetailAtPresenter;
import com.yanchangapp.ui.view.GoodsDetailAtView;
import com.zhy.view.flowlayout.FlowLayout;
import com.zhy.view.flowlayout.TagAdapter;
import com.zxl.baselib.ui.base.BaseActivity;
import com.zxl.baselib.util.ui.UIUtils;
import com.zxl.baselib.widget.MyTagFlowLayout;
import com.zxl.baselib.widget.YCLoadingView;

import butterknife.BindView;
import butterknife.ButterKnife;

public class GoodsDetailActivity extends BaseActivity<GoodsDetailAtView, GoodsDetailAtPresenter> implements GoodsDetailAtView {

    @BindView(R.id.ivToolbarNavigation)
    ImageView ivToolbarNavigation;
    @BindView(R.id.vToolbarDivision)
    View vToolbarDivision;
    @BindView(R.id.tvToolbarTitle)
    TextView tvToolbarTitle;
    @BindView(R.id.tvProductNameTitle)
    TextView tvProductNameTitle;
    @BindView(R.id.tvProductName)
    TextView tvProductName;
    @BindView(R.id.tvProductFormatTitle)
    TextView tvProductFormatTitle;
    @BindView(R.id.tvProductFormat)
    TextView tvProductFormat;
    @BindView(R.id.tvInventoryNumberTitle)
    TextView tvInventoryNumberTitle;
    @BindView(R.id.tvInventoryNumber)
    TextView tvInventoryNumber;
    @BindView(R.id.tvStoreRangeTitle)
    TextView tvStoreRangeTitle;
    @BindView(R.id.tvStoreRange)
    TextView tvStoreRange;
    @BindView(R.id.tvManufacturerTitle)
    TextView tvManufacturerTitle;
    @BindView(R.id.tvManufacturer)
    TextView tvManufacturer;
    @BindView(R.id.tagFlowLayout)
    MyTagFlowLayout tagFlowLayout;
    @BindView(R.id.btnNextRefresh)
    TextView btnNextRefresh;
    @BindView(R.id.llErrorNetwork)
    LinearLayout llErrorNetwork;
    @BindView(R.id.ycLoading)
    YCLoadingView ycLoading;
    @BindView(R.id.llContent)
    LinearLayout llContent;
    private String mProductID;
    private int isThirdFlag;
    private static final String PRODUCT_ID = "PRODUCT_ID";
    private static final String IS_THIRDFLAG = "isThirdFlag";

    @Override
    protected void init() {
        if (getIntent() != null && getIntent().hasExtra(PRODUCT_ID)) {
            mProductID = getIntent().getStringExtra(PRODUCT_ID);
            isThirdFlag = getIntent().getIntExtra(IS_THIRDFLAG,0);
        } else {
            finish();
        }
    }

    public static void show(Activity activity, String productID,int isThirdFlag) {
        Intent intent = new Intent(activity, GoodsDetailActivity.class);
        intent.putExtra(PRODUCT_ID, productID);
        intent.putExtra(IS_THIRDFLAG,isThirdFlag);
        activity.startActivity(intent);
    }

    @Override
    protected int provideContentViewId() {
        return R.layout.activity_goods_detail;
    }

    @Override
    protected GoodsDetailAtPresenter createPresenter() {
        return new GoodsDetailAtPresenter(this);
    }

    @Override
    protected void initView(Bundle savedInstanceState) {
        tvToolbarTitle.setText("品种详情");
    }

    @Override
    protected void initData() {
        // 设置加载...
        setLoadingStyle(LoadingType.LOADING);
        mPresenter.queryGoodsDetail(mProductID,isThirdFlag);
    }

    @Override
    protected void initListener() {
        ivToolbarNavigation.setOnClickListener(v -> finish());

        btnNextRefresh.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                initData();
            }
        });
    }

    private void setLoadingStyle(LoadingType loadingType){
        switch (loadingType){
            case LOADING:
                if (ycLoading.getVisibility() == View.GONE){
                    ycLoading.setVisibility(View.VISIBLE);
                }

                if (llContent.getVisibility() == View.VISIBLE){
                    llContent.setVisibility(View.GONE);
                }

                if (llErrorNetwork.getVisibility() == View.VISIBLE){
                    llErrorNetwork.setVisibility(View.GONE);
                }
                break;
            case SUCCESS:
                if (ycLoading.getVisibility() == View.VISIBLE){
                    ycLoading.setVisibility(View.GONE);
                }

                if (llContent.getVisibility() == View.GONE){
                    llContent.setVisibility(View.VISIBLE);
                }

                if (llErrorNetwork.getVisibility() == View.VISIBLE){
                    llErrorNetwork.setVisibility(View.GONE);
                }
                break;
            case ERROR:
                if (ycLoading.getVisibility() == View.VISIBLE){
                    ycLoading.setVisibility(View.GONE);
                }

                if (llContent.getVisibility() == View.VISIBLE){
                    llContent.setVisibility(View.GONE);
                }

                if (llErrorNetwork.getVisibility() == View.GONE){
                    llErrorNetwork.setVisibility(View.VISIBLE);
                }
                break;
        }
    }

    // 加载设置....

    @Override
    public void queryGoodsDetailSuccess(GoodsApplyBean bean) {
        tvProductName.setText(bean.getProductName());
        tvProductFormat.setText(bean.getProductFormat());
        tvManufacturer.setText(bean.getManufacturer());
        // 库存区间
        tvStoreRange.setText(getString(R.string.real_storage_range,bean.getAlarmMin(),bean.getAlarmMax()));
        tvInventoryNumber.setText(String.valueOf(bean.getInventoryNumber()));
        tagFlowLayout.setAdapter(new TagAdapter<SupplierBean>(bean.getSuppliers()) {

            @Override
            public View getView(FlowLayout parent, int position, SupplierBean supplierBean) {
                TextView tv = (TextView) LayoutInflater.from(GoodsDetailActivity.this)
                        .inflate(R.layout.tag_supplier_name,
                        parent, false);
                tv.setText(supplierBean.getSupplierName());
                return tv;
            }
        });
        setLoadingStyle(LoadingType.SUCCESS);
    }

    @Override
    public void queryGoodsDetailFailure(String err) {
        setLoadingStyle(LoadingType.ERROR);
        UIUtils.showToast(err);
    }

}
